package com.eliaukjq.jihe;

import java.util.*;

public class Maina {
    public static void main(String[] args) {
        Set<Employee> set1 = new TreeSet<Employee>();
        set1.add(new Employee("TomA", 15));
        set1.add(new Employee("Tom11", 20));  //抛出ClassCastException
        set1.add(new Employee("Tom23", 20));
        set1.add(new Employee("TomD", 20));
        Iterator<Employee> iterator1 = set1.iterator();
        while (iterator1.hasNext()) {
            System.out.println(iterator1.next().getName());
        }
    }
}

class Employee implements Comparable<Employee> {
    private String name;
    private Integer age;

    public int compareTo(Employee other) {
        //先按照name属性排序
        if (this.name.compareTo(other.getName()) > 0) return 1;
        if (this.name.compareTo(other.getName()) < 0) return -1;

        //再按照age属性排序
        if (this.age > other.getAge()) return 1;
        if (this.age < other.getAge()) return -1;
        return 0;
    }
    public Employee(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
